package business.core.service.bus;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import business.core.mybatis.dao.BusMapper;
import business.core.mybatis.model.Bus;
import business.core.mybatis.model.BusExample;
import business.core.mybatis.page.Page;
import business.core.service.ExceptionStataicCode;

@Component
public class BusService {
	@Autowired
	public BusMapper busMapper;
	
	
	public Page<Bus> queryBusByExample(Page<Bus> page,BusExample example){
		page.setObjects(busMapper.selectByExample(example));
		page.setCount(busMapper.countByExample(example));
		return page;
	}
	
	public void addBus(Bus bus) throws Exception{
		BusExample example = new BusExample();
		example.createCriteria().andNumberEqualTo(bus.getNumber());
		this.isExistsSameBusName(bus.getNumber(),example);
		
		busMapper.insert(bus);
	}
	
	public void updateBus(Bus bus) throws Exception{
		BusExample example = new BusExample();
		example.createCriteria().andNumberEqualTo(bus.getNumber()).andIdNotEqualTo(bus.getId());
		this.isExistsSameBusName(bus.getNumber(), example);
		
		busMapper.updateByPrimaryKeySelective(bus);
	}
	
	
	public void isExistsSameBusName(String busName,BusExample example) throws Exception{
		List<Bus> buss = busMapper.selectByExample(example);
		if(buss!=null&&buss.size()>0){
			throw new Exception(ExceptionStataicCode.EXISTS_SAME_DATA_IN_DATABASE);
		}
	}
	
	public Bus getBusById(String id){
		return busMapper.selectByPrimaryKey(id);
	}
}
